﻿
var MrGraphieBundler = function () {
    var bundle = function (graphData) {
        var bundleParameters = [];

        for (var i in graphData.graphParams) {
            if (graphData.graphParams.hasOwnProperty(i)) {
                var elem = {
                    key: i,
                    value: graphData.graphParams[i]
                };
            }
            bundleParameters.push(elem);
        }

        var graphBundle = {
            id: graphData.id ? graphData.id : null,
            name: graphData.name,
            type: graphData.type,
            form: graphData.form,
            step: graphData.step,
            lineColor: graphData.lineColor,
            lineWidth: graphData.lineWidth,
            pointSize: graphData.pointSize,
            parameters: bundleParameters
        }
        return graphBundle;
    },

    unbundle = function (bundleData, templateLibrary) {
        var graphData = new templateLibrary.getTemplate(bundleData.type);

        graphData.id = bundleData.id;
        graphData.name = bundleData.name;
        graphData.type = bundleData.type;
        graphData.form = bundleData.form;
        graphData.step = bundleData.step;
        graphData.lineColor = bundleData.lineColor;
        graphData.lineWidth = bundleData.lineWidth;
        graphData.pointSize = bundleData.pointSize;

        
        var bundleParameters = {};

        for (var i in bundleData.parameters) {
            if(bundleData.parameters.hasOwnProperty(i))
            {
                bundleParameters[bundleData.parameters[i].key] = bundleData.parameters[i].value;
            }
        }

        graphData.graphParams = bundleParameters;

        return graphData;
    };

    return {
        bundle: bundle,
        unbundle: unbundle
    };
};